$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/onshelves/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
			{ label: '上架单号', name: 'uppernumber', index: 'upperNumber', width: 110 },
			{ label: '货架', name: 'shelvesid', index: 'shelvesid', width: 110,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.shelvesid!=null&&rowdata.shelvesid!=""){
                        var shelvesname = "";

                        $.ajax({
                            type: "POST",
                            async:false,
                            url: baseURL + "sys/goodshelves/info/"+rowdata.shelvesid,
                            success: function(r){
                                shelvesname =r.goodshelves.shelvesname;

                            }
                        });

                        return shelvesname;
                    }else{
                        return '';
                    }


                } },
            { label: '商品种类', name: 'type', index: 'type', width: 60 },
            { label: '商品数量', name: 'num', index: 'num', width: 60 },
            { label: '商品成本', name: 'cost', index: 'cost', width: 60 },
			{ label: '创建时间', name: 'createtime', index: 'createtime', width: 90 },
			{ label: '创建人', name: 'createuser', index: 'createUser', width: 60,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.createuser!=null&&rowdata.createuser!=""){
                        var username = "";

                        $.ajax({
                            type: "POST",
                            async:false,
                            url: baseURL + "sys/user/info/"+rowdata.createuser,
                            success: function(r){
                                username =r.user.username;


                            }
                        });

                        return username;
                    }else{
                        return '';
                    }


                } },
			{ label: '状态', name: 'statu', index: 'statu', width: 60,formatter: function(item, index) {
                    if (item === 0) {
                        return '待上架';
                    }
                    if (item === 1) {
                        return '已完成';
                    }
                    if (item === 2) {
                        return '已作废';
                    }
                } },
            { label: '备注', name: 'remark', index: 'remark', width: 80,formatter: function(cellvalue, options, rowdata) {
            	if(rowdata.remark!=null&&rowdata.remark!=""){
            		return rowdata.remark;
				}else{
            		return "--";
				}
            } },
            { label: '操作', name: 'sy', width: 100,align:'center',formatter: function(cellvalue, options, rowdata) {
                if(rowdata.statu == 0){
                    return "<a style='cursor:pointer;' onclick='vm.getDetails("+ rowdata.id +","+rowdata.statu+")'>" +
                        "详情</a>&nbsp;&nbsp;&nbsp;&nbsp;" +
                        "<a style='cursor:pointer;' onclick='vm.opera("+ rowdata.id +",1)'>"+
                        "上架</a>&nbsp;&nbsp;&nbsp;&nbsp;"+
                        "<a style='cursor:pointer;' onclick='vm.opera("+ rowdata.id +",2)'>" +
                        "作废</a>";
                }else{
                    return "<a style='cursor:pointer;' onclick='vm.getDetails("+ rowdata.id +","+rowdata.statu+")'>" +
                        "详情</a>";
                }

                }}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    vm.Statistics(null,null,null,null,null);
    vm.queryGoodshelves();
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
            shelvesid:null,
            number:null,
            reason:null,
            startime:null,
            endtime:null
		},
        qs:{
            sname:null,
            groupid:null
        },
        qp:{
            name:null,
            categoryid:null
        },
		showList: true,
        showInfoList:true,
        showAddList:true,
		title: null,
		onshelves: {},
        childShelves:[],
        childProduct:[],
        statu:null
	},
	methods: {
		query: function () {

			vm.reload();
		},
        queryShelves: function () {
            vm.reloadShelves();
        },
        queryProduct: function () {
            vm.reloadProduct();
        },
		add: function(){
			vm.showList = false;
            vm.showAddList = false;
			vm.title = "新增";
			vm.onshelves = {};
            $("#shevleves").html("");
            $("#goods").html("");
            $("#zl").text('0');
            $("#sl").text('0');
            $("#zje").text('￥0.00');
            vm.statu = null;
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
        Statistics: function (shelvesid,number,statu,startime,endtime) {
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/onshelves/infoStatistics",
                data:{'shelvesid':shelvesid,'number':number,'statu':statu,'startime':startime,'endtime':endtime},
                success: function(r){
                    $("#totalcount").text(r.onshelves.totalcount);
                    $("#totalnum").text(r.onshelves.totalnum==null?0:r.onshelves.totalnum);
                    $("#totalprice").text(r.onshelves.totalprice==null?"￥"+0:"￥"+r.onshelves.totalprice);

                }
            });
        },
        opera: function (id,statu) {
		    vm.getInfo(id);
		    vm.onshelves.statu = statu;
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/onshelves/update",
                contentType: "application/json",
                data: JSON.stringify(vm.onshelves),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
		addShelves: function () {
            $("#jqGridAdd").jqGrid({
                url: baseURL + 'sys/goodshelves/listByParams',
                datatype: "json",
                colModel: [
                    { label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
                    { label: '货架名称', name: 'shelvesname', index: 'shelvesName', width: 170 },
                    { label: '货架地址', name: 'shelvesaddress', index: 'shelvesAddress', width: 225 },
                    { label: '货架分组', name: 'shelvesgroup', index: 'shelvesGroup',width: 170,formatter: function(cellvalue, options, rowdata) {
                            if(rowdata.shelvesgroup!=null&&rowdata.shelvesgroup!=""){
                                var groupname = "";
                                var arr = rowdata.shelvesgroup.split(",");
                                for(var i=0;i<arr.length;i++){
                                    $.ajax({
                                        type: "POST",
                                        async:false,
                                        url: baseURL + "sys/shelvesgroup/info/"+parseInt(arr[i]),
                                        success: function(r){
                                            groupname +=r.shelvesgroup.groupname;
                                            if(i !=arr.length-1){
                                                groupname +="/";
                                            }

                                        }
                                    });
                                }
                                return groupname;
                            }else{
                                return '未分组';
                            }


                        } }
                ],
                viewrecords: true,
                height: 385,
                rowNum: 10,
                rowList : [10,30,50],
                rownumbers: true,
                rownumWidth: 25,
                autowidth:true,
                multiselect: true,
                pager: "#jqGridPagerAdd",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    $("#jqGridPagerAdd").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                }
            });
            vm.queryGroup();
            $('#myModal').modal();
        },
        addProduct: function (statu) {
            var s = $("#shevleves").html();
            if(s!=""||statu==0){
                $("#jqGridAddProudct").jqGrid({
                    url: baseURL + 'sys/product/list',
                    datatype: "json",
                    colModel: [
                        { label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
                        { label: '商品名称', name: 'productname', index: 'productName', width: 170 },
                        { label: '分类', name: 'categoryid', index: 'categoryid', width: 225,formatter: function(cellvalue, options, rowdata) {
                                if(rowdata.categoryid!=null&&rowdata.categoryid!=""){
                                    var groupname = "";
                                    var arr = rowdata.categoryid.split(",");
                                    for(var i=0;i<arr.length;i++){
                                        $.ajax({
                                            type: "POST",
                                            async:false,
                                            url: baseURL + "sys/productcategory/info/"+parseInt(arr[i]),
                                            success: function(r){
                                                groupname +=r.productcategory.name;
                                                if(i !=arr.length-1){
                                                    groupname +="/";
                                                }

                                            }
                                        });
                                    }
                                    return groupname;
                                }else{
                                    return '未分组';
                                }


                            } },
                        { label: '创建时间', name: 'createtime', index: 'createtime',width: 170}
                    ],
                    viewrecords: true,
                    height: 385,
                    rowNum: 10,
                    rowList : [10,30,50],
                    rownumbers: true,
                    rownumWidth: 25,
                    autowidth:true,
                    multiselect: true,
                    pager: "#jqGridPagerAddProudct",
                    jsonReader : {
                        root: "page.list",
                        page: "page.currPage",
                        total: "page.totalPage",
                        records: "page.totalCount"
                    },
                    prmNames : {
                        page:"page",
                        rows:"limit",
                        order: "order"
                    },
                    gridComplete:function(){
                        //隐藏grid底部滚动条
                        $("#jqGridPagerAddProudct").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                    }
                });
                vm.queryCategory();
                $('#myProductModal').modal();
            }else{
                alert("请添加货架");
            }

        },
        showShelves:function () {
            var grid = $("#jqGridAdd");
            var rowKey = grid.getGridParam("selrow");
            if(!rowKey){
                alert("请选择一条记录");
                return ;
            }
            var rows = grid.getGridParam("selarrrow");

            var temp = "";
            $("#shevleves").html("");
            for(var i=0;i<rows.length;i++){
                var obj = {};
                var isExits = false;
                if(vm.childShelves.length>0){
                    for(var j=0;j<vm.childShelves.length;j++){
                        if(rows[i]==vm.childShelves[j].id){
                            isExits = true;
                        }
                    }
                    if(!isExits){
                        obj.id = rows[i];
                        obj.name = grid.getRowData(rows[i]).shelvesname;
                        vm.childShelves.push(obj);

                    }

                }else{
                    obj.id = rows[i];
                    obj.name = grid.getRowData(rows[i]).shelvesname;
                    vm.childShelves.push(obj);


                }

            }
            for(var k=0;k<vm.childShelves.length;k++){
                temp +="<a class='btn' style='color: #333;border-color: #ccc;cursor:default;' " +
                    "id='"+vm.childShelves[k].id+"'>"+vm.childShelves[k].name+"</a>" +
                    "<img src='../../statics/img/ico15-close.png' onclick='vm.removeShelves("+vm.childShelves[k].id+")'" +
                    "style='z-index:12;width:13px;height:13px;margin-left:-14px;margin-top:-15px;cursor: pointer; ' />&nbsp;&nbsp;";
            }
            $("#shevleves").append(temp);
            $('#myModal').modal("hide");

        },

        showProduct:function () {
            var grid = $("#jqGridAddProudct");
            var rowKey = grid.getGridParam("selrow");
            if(!rowKey){
                alert("请选择一条记录");
                return ;
            }
            var rows = grid.getGridParam("selarrrow");

            var temp = "";
            $("#goods").html("");
            var productcount = 0;
            var totalprice = 0;
            console.log(vm.childProduct)
            for(var i=0;i<rows.length;i++){
                var obj = {};
                var isExits = false;
                if(vm.childProduct.length>0){
                    for(var j=0;j<vm.childProduct.length;j++){
                        if(rows[i]==vm.childProduct[j].id){
                            isExits = true;
                        }
                    }
                    if(!isExits){
                        $.ajax({
                            type: "POST",
                            async:false,
                            url: baseURL + "sys/product/info/"+rows[i],
                            success: function(r){
                                obj.id = rows[i];
                                obj.name = grid.getRowData(rows[i]).productname;
                                obj.headimg = r.product.productimg;
                                obj.costprice = r.product.costprice;
                                obj.sellprice = r.product.sellprice;
                                obj.count = 1;
                                vm.childProduct.push(obj);

                            }
                        });

                    }

                }else{
                    $.ajax({
                        type: "POST",
                        async:false,
                        url: baseURL + "sys/product/info/"+rows[i],
                        success: function(r){
                            obj.id = rows[i];
                            obj.name = grid.getRowData(rows[i]).productname;
                            obj.headimg = r.product.productimg;
                            obj.costprice = r.product.costprice;
                            obj.sellprice = r.product.sellprice;
                            obj.count = 1;
                            vm.childProduct.push(obj);

                        }
                    });



                }

            }

            for(var k=0;k<vm.childProduct.length;k++){
                temp +="<tr style='height:39px;' ><td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);'><img src='"+vm.childProduct[k].headimg+"'></td>" +
                    "<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>"+vm.childProduct[k].name+"</td>" +
                    "<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>" +
                    "<input type='text' style='width:50px;text-indent:10px;border: solid 1px #d2d6de;' value='"+vm.childProduct[k].count+"' onchange=vm.changeInput("+vm.childProduct[k].id+",this,'sl')></td>" +
                    "<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>" +
                    "<div style='display: inline-block;border: solid 1px #d2d6de;width:70px;'>"+
                    "<span style='top: 8px;left: 10px;'>￥</span>" +
                    "<input type='text' style='width:40px;border-style:none;outline:medium;' value='"+vm.childProduct[k].costprice+"' onchange=vm.changeInput("+vm.childProduct[k].id+",this,'cb')></div></td>" +
                    "<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>" +
                    "<div style='display: inline-block;border: solid 1px #d2d6de;width:70px;'>"+
                    "<span style='top: 8px;left: 10px;'>￥</span>" +
                    "<input type='text' style='width:40px;border-style:none;outline:medium;' value='"+vm.childProduct[k].sellprice+"' onchange=vm.changeInput("+vm.childProduct[k].id+",this,'xsj')></div></td>" +
                    "<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>￥" +
                    "<span id='total"+vm.childProduct[k].id+"'>"+vm.childProduct[k].count*vm.childProduct[k].costprice+"</span></td>";
                    if(vm.statu!=0){
                        temp +="<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>" +
                            "10&nbsp;<img src='../../statics/img/ico20-question.png' onclick='vm.showStock("+vm.childProduct[k].id+")' style='cursor: pointer;width: 15px;height: 15px;'></td>";
                    }
                    temp +="<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>" +
                    "<img src='../../statics/img/ico16-delete.png' onclick='vm.removeProduct("+vm.childProduct[k].id+")' style='cursor: pointer'></td></tr>";

                productcount +=vm.childProduct[k].count;
                totalprice += vm.childProduct[k].count*vm.childProduct[k].costprice;
            }
            if(vm.statu == 0){
                $("#spzl").text(vm.childProduct.length);
                $("#spsl").text(productcount);
                $("#hjcbje").text(totalprice);

                $("#infoproduct").html(temp);
            }else{
                $("#zl").text(vm.childProduct.length);
                $("#sl").text(productcount);
                $("#zje").text(totalprice);
                $("#goods").append(temp);
            }

            $('#myProductModal').modal("hide");

        },
        removeShelves: function (id) {
            var temp = "";
            if(vm.childShelves.length==1 && vm.childProduct.length>0){
                alert("至少选择一个货架");
                return;
            }
            $("#shevleves").html("");
            var childShelvestemp = [];
            for(var i=0;i<vm.childShelves.length;i++){
                var obj = {};

                if(vm.childShelves[i].id !=id){
                    obj.id = vm.childShelves[i].id;
                    obj.name = vm.childShelves[i].name
                    childShelvestemp.push(obj);

                    temp +="<a class='btn' style='color: #333;border-color: #ccc;cursor:default;' " +
                        "id='"+vm.childShelves[i].id+"'>"+vm.childShelves[i].name+"</a>" +
                        "<img src='../../statics/img/ico15-close.png' onclick='vm.removeShelves("+vm.childShelves[i].id+")'" +
                        "style='z-index:12;width:13px;height:13px;margin-left:-14px;margin-top:-15px;cursor: pointer; ' />&nbsp;&nbsp;";
                }

            }
            vm.childShelves = childShelvestemp;
            $("#shevleves").append(temp);
        },
        removeProduct: function (id) {
            var temp = "";
            $("#goods").html("");
            var childProducttemp = [];
            var productcount = 0;
            var totalprice = 0;
            console.log(vm.childProduct)
            console.log(id)
            for(var i=0;i<vm.childProduct.length;i++){
                var obj = {};

                if(vm.childProduct[i].id !=id){
                    /*$.ajax({
                        type: "POST",
                        async:false,
                        url: baseURL + "sys/product/info/"+vm.childProduct[i].id,
                        success: function(r){*/
                            obj.id = vm.childProduct[i].id;
                            obj.name = vm.childProduct[i].name;
                            obj.headimg = vm.childProduct[i].headimg;
                            obj.costprice = vm.childProduct[i].costprice;
                            obj.sellprice = vm.childProduct[i].sellprice;
                            obj.count = vm.childProduct[i].count;
                            childProducttemp.push(obj);
                            productcount +=vm.childProduct[i].count;
                            totalprice += vm.childProduct[i].count*vm.childProduct[i].costprice;
                        /*}
                    });*/

                    temp +="<tr style='height:39px;' ><td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);'><img src='"+vm.childProduct[i].headimg+"'></td>" +
                        "<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>"+vm.childProduct[i].name+"</td>" +
                        "<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>" +
                        "<input type='text' style='width:50px;text-indent:10px;border: solid 1px #d2d6de;' value='"+vm.childProduct[i].count+"' onchange=vm.changeInput("+vm.childProduct[i].id+",this,'sl')></td>" +
                        "<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>" +
                        "<div style='display: inline-block;border: solid 1px #d2d6de;width:70px;'>"+
                        "<span style='top: 8px;left: 10px;'>￥</span>" +
                        "<input type='text' style='width:40px;border-style:none;outline:medium;' value='"+vm.childProduct[i].costprice+"' onchange=vm.changeInput("+vm.childProduct[i].id+",this,'cb')></div></td>" +
                        "<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>" +
                        "<div style='display: inline-block;border: solid 1px #d2d6de;width:70px;'>"+
                        "<span style='top: 8px;left: 10px;'>￥</span>" +
                        "<input type='text' style='width:40px;border-style:none;outline:medium;' value='"+vm.childProduct[i].sellprice+"' onchange=vm.changeInput("+vm.childProduct[i].id+",this,'xsj')></div></td>" +
                        "<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>￥" +
                        "<span id='total"+vm.childProduct[i].id+"'>"+vm.childProduct[i].count*vm.childProduct[i].costprice+"</span></td>";
                        if(vm.statu !=0){
                            temp +="<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>" +
                                "10&nbsp;<img src='../../statics/img/ico20-question.png' onclick='vm.showStock("+vm.childProduct[k].id+")' style='cursor: pointer;width: 15px;height: 15px;'></td>";
                        }
                        temp +="<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>" +
                        "<img src='../../statics/img/ico16-delete.png' onclick='vm.removeProduct("+vm.childProduct[i].id+")' style='cursor: pointer'></td></tr>";
                }

            }
            vm.childProduct = childProducttemp;
            if(vm.statu == 0){
                $("#spzl").text(vm.childProduct.length);
                $("#spsl").text(productcount);
                $("#hjcbje").text(totalprice);

                $("#infoproduct").html(temp);
            }else{
                $("#zl").text(vm.childProduct.length);
                $("#sl").text(productcount);
                $("#zje").text(totalprice);
                if(vm.childProduct.length==0){
                    temp +="<tr style='height:39px;' ><td colspan='8' style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);'>您还未选择商品</td></tr>";
                }
                $("#goods").append(temp);
            }

        },
        showStock: function (id) {
            $("#jqGridProudctStock").jqGrid({
                url: baseURL + 'sys/productshelvesstcok/infostock/'+id,
                datatype: "json",
                colModel: [
                    { label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
                    { label: '货架', name: 'shelvesname', index: 'shelvesname', width: 180 },
                    { label: '当前库存数量', name: 'stock', index: 'stock', width: 180 },
                    { label: '销售价', name: 'sellprice', index: 'sellprice', width: 180 }
                ],
                viewrecords: true,
                height: 385,
                rowNum: -1,
                rownumbers: true,
                rownumWidth: 25,
                autowidth:true,
                multiselect: true,
                jsonReader : {
                    root: "productshelvesstcok"
                },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    //$("#jqGridPagerProudctStock").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                }
            });
            $('#productStockModal').modal();
        },
        changeInput: function (id,e,p) {
		    var productcount = 0;
		    var totalprice = 0;
            for(var k=0;k<vm.childProduct.length;k++){
                if(vm.childProduct[k].id == id){
                    if(p == 'sl'){
                        vm.childProduct[k].count = e.value;
                    }
                    if(p == 'cb'){
                        vm.childProduct[k].costprice = e.value;
                    }
                    if(p == 'xsj'){
                        vm.childProduct[k].sellprice = e.value;
                    }
                    $("#total"+id).text(vm.childProduct[k].count*vm.childProduct[k].costprice);
                }
                productcount += parseInt(vm.childProduct[k].count);
                totalprice += vm.childProduct[k].count*vm.childProduct[k].costprice;
            }
            if(vm.statu ==0){
                $("#spsl").text(productcount);
                $("#hjcbje").text(totalprice);
            }else{
                $("#sl").text(productcount);
                $("#zje").text(totalprice);
            }


        },
        getDetails:function (id,statu) {
		    vm.statu = statu;
		    vm.getInfo(id);
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/onshelves/infoById/"+id,
                success: function(r){
                    $("#uppernumber").text(r.onshelves.uppernumber);
                    $("#createtime").text(r.onshelves.createtime);
                    $("#lasttime").text(r.onshelves.lasttime);
                    $("#createuser").text(r.onshelves.createuser);
                    $("#shelvesname").text(r.onshelves.shelvesname);
                    $("#lastuser").text(r.onshelves.lastuser);
                    $("#remark").text(r.onshelves.remark==''?'--':r.onshelves.remark);
                }
            });
            if(statu == 0){
                $("#addBtn").show();
                $("#bccg").show();
                $("#zjsj").show();
                $("#cz").show();
            }else{
                $("#addBtn").hide();
                $("#bccg").hide();
                $("#zjsj").hide();
                $("#cz").hide();
            }

            $("#infoproduct").html("");
            var temp = "";
            vm.childProduct=[];
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/onshelvesitem/infoByShelvesId/"+id,
                success: function(r){
                    var spsl = 0;
                    var hjcbje = 0;
                    var obj = {};
                   for(var i=0;i<r.onshelvesitem.length;i++){
                       spsl += r.onshelvesitem[i].oncount;
                       hjcbje += r.onshelvesitem[i].totalcost;
                       obj.id = r.onshelvesitem[i].id;
                       obj.name = r.onshelvesitem[i].productname;
                       obj.headimg = r.onshelvesitem[i].productimg;
                       obj.costprice = r.onshelvesitem[i].costprice;
                       obj.sellprice = r.onshelvesitem[i].sellprice;
                       obj.count = r.onshelvesitem[i].oncount;
                       vm.childProduct.push(obj);
                       if(statu == 0){
                           temp +="<tr style='height:39px;' ><td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);'><img src='"+r.onshelvesitem[i].productimg+"'></td>" +
                               "<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>"+r.onshelvesitem[i].productname+"</td>" +
                               "<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>" +
                               "<input type='text' style='width:50px;text-indent:10px;border: solid 1px #d2d6de;' value='"+r.onshelvesitem[i].oncount+"' onchange=vm.changeInput("+r.onshelvesitem[i].id+",this,'sl')></td>" +
                               "<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>" +
                               "<div style='display: inline-block;border: solid 1px #d2d6de;width:70px;'>"+
                               "<span style='top: 8px;left: 10px;'>￥</span>" +
                               "<input type='text' style='width:40px;border-style:none;outline:medium;' value='"+r.onshelvesitem[i].costprice+"' onchange=vm.changeInput("+r.onshelvesitem[i].id+",this,'cb')></div></td>" +
                               "<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>" +
                               "<div style='display: inline-block;border: solid 1px #d2d6de;width:70px;'>"+
                               "<span style='top: 8px;left: 10px;'>￥</span>" +
                               "<input type='text' style='width:40px;border-style:none;outline:medium;' value='"+r.onshelvesitem[i].sellprice+"' onchange=vm.changeInput("+r.onshelvesitem[i].id+",this,'xsj')></div></td>" +
                               "<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>￥" +
                               "<span id='total"+r.onshelvesitem[i].id+"'>"+1*r.onshelvesitem[i].totalcost+"</span></td>" +
                               "<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>" +
                               "<img src='../../statics/img/ico16-delete.png' onclick='vm.removeProduct("+r.onshelvesitem[i].id+")' style='cursor: pointer'></td></tr>";
                       }else{
                           temp +="<tr style='height:39px;' ><td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);'><img src='"+r.onshelvesitem[i].productimg+"'></td>" +
                               "<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>"+r.onshelvesitem[i].productname+"</td>" +
                               "<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>"+r.onshelvesitem[i].oncount+"</td>" +
                               "<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>￥"+r.onshelvesitem[i].costprice+"</td>" +
                               "<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>￥"+r.onshelvesitem[i].sellprice+"</td>" +
                               "<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>￥"+r.onshelvesitem[i].totalcost+"</td></tr>";
                       }

                   }
                    $("#spzl").text(r.onshelvesitem.length);
                    $("#spsl").text(spsl);
                    $("#hjcbje").text(hjcbje);
                }
            });

            $("#infoproduct").html(temp);
            vm.showList = false;
            vm.showInfoList = false;


        },
		saveOrUpdate: function (statu,temp) {

            vm.onshelves.statu = statu;
            vm.onshelves.products = vm.childProduct;
            var url = "sys/onshelves/update";
            if(temp){
                vm.onshelves.shelvesid = vm.childShelves;
                url = "sys/onshelves/save";
            }
            if(vm.childShelves.length==0&&temp){
                alert("货架不能为空");
            }else if(vm.childProduct.length==0){
                alert("货架商品不能为空");
            }else{
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.onshelves),
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(index){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            }

		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/onshelves/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
        queryGoodshelves: function(){
            $("#sheelves_id").html("");
            $.get(baseURL + "sys/goodshelves/alllist", function(r){
                var temp = "<option value=''></option>";
                $.each(r.alllist, function (i) {

                    temp +="<option value='"+r.alllist[i].id+"'>"+r.alllist[i].shelvesname+"</option>";

                });
                $("#sheelves_id").append(temp);

            });
        },
        queryGroup: function(){
            $("#s_id").html("");
            $.get(baseURL + "sys/shelvesgroup/alllist", function(r){
                var temp = "<option value=''></option><option value='0'>未分组</option>";
                $.each(r.alllist, function (i) {

                    temp +="<option value='"+r.alllist[i].id+"'>"+r.alllist[i].groupname+"</option>";

                });
                $("#s_id").append(temp);

            });
        },
        queryCategory: function(){
            $("#p_id").html("");
            $.get(baseURL + "sys/productcategory/alllist", function(r){
                var temp = "<option value=''></option><option value='0'>未分组</option>";
                $.each(r.alllist, function (i) {

                    temp +="<option value='"+r.alllist[i].id+"'>"+r.alllist[i].name+"</option>";

                });
                $("#p_id").append(temp);

            });
        },
		getInfo: function(id){
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/onshelves/info/"+id,
                success: function(r){
                    vm.onshelves = r.onshelves;
                }
            });
		},
		reload: function () {
            var startime = $("#startime").val();
            var endtime = $("#endtime").val();
            if((startime!=''&&typeof startime!='undefined')&&(endtime!=''&&typeof endtime!='undefined')){
                var st = new Date(startime).getTime();
                var et = new Date(endtime).getTime();
                if(st>et){
                    alert("开始时间不能大于结束时间");
                    return;
                }
            }
            vm.Statistics(vm.q.shelvesid,vm.q.number,vm.q.statu,startime,endtime);
			vm.showList = true;
			vm.showInfoList = true;
            vm.showAddList = true;
            if(endtime!=''&&typeof endtime!='undefined'){
                var date = new Date(new Date(endtime).getTime()+(24*60*60*1000));
                var premonth   = date.getMonth()+1; //获取当前月份(0-11,0代表1月)
                if(premonth<10){
                    premonth = "0"+premonth;
                }
                var preday = date.getDate(); //获取当前日(1-31)
                if(preday<10){
                    preday = "0"+preday;
                }
                endtime = date.getFullYear()+"-"+premonth+"-"+preday;
            }
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'shelvesid':vm.q.shelvesid,'number':vm.q.number,'statu':vm.q.statu,'startime':startime,'endtime':endtime},
                page:page
            }).trigger("reloadGrid");

		},
        reloadShelves: function (event) {
            var page = $("#jqGridAdd").jqGrid('getGridParam','page');
            $("#jqGridAdd").jqGrid('setGridParam',{
                postData:{'sname':vm.qs.sname,'groupid':vm.qs.groupid},
                page:page
            }).trigger("reloadGrid");
        },
        reloadProduct: function (event) {
            var page = $("#jqGridAddProudct").jqGrid('getGridParam','page');
            $("#jqGridAddProudct").jqGrid('setGridParam',{
                postData:{'name': vm.qp.name,'categoryid':vm.qp.categoryid},
                page:page
            }).trigger("reloadGrid");
        }
	}
});